//
//  MeViewController.m
//  DBAChain
//
//  Created by Evan on 2018/9/4.
//  Copyright © 2018 Evan. All rights reserved.
//

#import "MeViewController.h"
#import "LoginNavigationController.h"
#import "MePageHeadView.h"
#import "AssetsListTableViewCell.h"
#import "MeSetViewController.h"
#import "UserCenter.h"
#import "taskUserManagerService.h"
#import "MeMessageViewController.h"
#import "ShareFriendViewController.h"
#import "MeFeedbackViewController.h"
#import "MeSourceCenterViewController.h"
#import "MeAddressViewController.h"
#import "MeCallViewController.h"
#import "MeGradeViewController.h"
#import "MeSourceDetailViewController.h"

@interface MeViewController ()<UITableViewDelegate,UITableViewDataSource>
@property (nonatomic,strong)UITableView    *tableView;
@property (nonatomic,strong)MePageHeadView *headView;
@property (nonatomic,strong)NSArray        *cellArray;
@end

@implementation MeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navigationController.title = @"个人中心";
    
    self.cellArray = @[@[@"合伙人申请",@"分享推广",@"行权中心",@"行权订单",@"收货地址",@"消息中心",@"联系我们",@"反馈"],@[@"icon_grade",@"icon_me_mo",@"icon_me_gte",@"icon_me_drafts",@"icon_me_users",@"icon_me_lists",@"icon_me_flag",@"icon_me_message"]];
    [self.view addSubview:self.tableView];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    
    if (@available(iOS 11.0, *)) {
        UIScrollView.appearance.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    } else {
        self.automaticallyAdjustsScrollViewInsets = NO;
    }
    
    [self setBlackStatus:NO];
    
    self.hidesBottomBarWhenPushed = NO;
    if (self.tabBarController.tabBar.hidden == YES) {
        self.tabBarController.tabBar.hidden = NO;
    }
    if (self.navigationController.navigationBar.hidden == NO) {
        self.navigationController.navigationBar.hidden = YES;
    }
   
    BOOL isLogin = [[UserCenter shareCenter] isLogined];
    if (isLogin) {
        [[UserCenter shareCenter] refreshProfiles:^(dataGetUserInfo *profile) {
            [self loginIfSuccess];
        }];
       
    }
}

- (instancetype)init
{
    self = [super init];
    if (self) {
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(loginIfSuccess) name:Notification_login_ok_notice object:nil];
    }
    return self;
}


-(void)loginIfSuccess
{
    self.headView.lbname.text = [UserCenter shareCenter].user.userName != nil ?  [UserCenter shareCenter].user.userName : @"会员";
    if ([[UserCenter shareCenter].user.IsIdentification isEqualToString:@"20060002"]) {
        self.headView.labelStartRZ.text =  @"已认证";
    }
    self.headView.lbnumber.text = [UserCenter shareCenter].user.UserId;
    self.headView.urlImage.layer.masksToBounds = YES;
    self.headView.urlImage.layer.borderWidth = 1.0f;
    self.headView.urlImage.layer.borderColor = [UIColor clearColor].CGColor;
    self.headView.urlImage.layer.cornerRadius = 30.0;
    [self.headView.urlImage sd_setImageWithURL:[NSURL URLWithString:[UserCenter shareCenter].user.imageUrl] placeholderImage:[UIImage imageNamed:@"icon_icon"]];
    [self.headView.btnUserType setTitle:[UserCenter shareCenter].user.UserTypeName forState:UIControlStateNormal];
}

#pragma mark - init
-(UITableView *)tableView
{
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, KScreenHeight-64) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.backgroundColor  = [UIColor whiteColor];
        _tableView.scrollEnabled = YES;
        _tableView.estimatedSectionFooterHeight = 0;
        _tableView.estimatedSectionHeaderHeight = 0;
    }
    return _tableView;
}

-(MePageHeadView *)headView
{
    if (!_headView) {
        _headView = [MePageHeadView getHeadView];
        [_headView.setButton addTarget:self action:@selector(setAction) forControlEvents:UIControlEventTouchUpInside];
    }
    return _headView;
}

#pragma mark - event
-(void)setAction
{
    [self.navigationController pushViewController:[MeSetViewController new] animated:YES];
}

#pragma mark - UITableView
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return [self.cellArray[0] count];
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 80;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section
{
    UIView *footerView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, 80)];
    footerView.backgroundColor = [UIColor clearColor];
    UIButton *footerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    footerButton.frame = CGRectMake(60, 35, KScreenWidth - 120, 40);
    footerButton.backgroundColor = COLOR_main;
    footerButton.layer.masksToBounds = YES;
    footerButton.layer.borderWidth = 1.0f;
    footerButton.layer.borderColor = [UIColor clearColor].CGColor;
    footerButton.layer.cornerRadius = 20.0f;
    footerButton.titleLabel.font = [UIFont systemFontOfSize:15];
    [footerButton setTitle:@"退出登录" forState:UIControlStateNormal];
    [footerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [footerButton addTarget:self action:@selector(loginOut) forControlEvents:UIControlEventTouchUpInside];
    [footerView addSubview:footerButton];
    return footerView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return 200;
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    return self.headView;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    AssetsListTableViewCell *cell = [AssetsListTableViewCell cellWithTableView:tableView];
    cell.lbtitle.text = self.cellArray[0][indexPath.row];
    cell.iamgeView.image = [UIImage imageNamed:self.cellArray[1][indexPath.row]];
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row == 0) {
        [self.navigationController pushViewController:[MeGradeViewController new] animated:YES];
    }else if (indexPath.row == 1) {
        [self.navigationController pushViewController:[ShareFriendViewController new] animated:YES];
    }else if (indexPath.row == 2 ) {
        [self.navigationController pushViewController:[MeSourceCenterViewController new] animated:YES];
    }else if (indexPath.row == 3) {
        [self.navigationController pushViewController:[MeSourceDetailViewController new] animated:YES];
    }else if (indexPath.row == 4) {
        [self.navigationController pushViewController:[MeAddressViewController new] animated:YES];
    }else if (indexPath.row == 5) {
        [self.navigationController pushViewController:[MeMessageViewController new] animated:YES];
    }else if (indexPath.row == 6){
        [self.navigationController pushViewController:[MeCallViewController new] animated:YES];
    }else if (indexPath.row == 7){
        [self.navigationController pushViewController:[MeFeedbackViewController new] animated:YES];
    }
}

-(void)loginOut{
    [[UIAlertController alloc] alertTitle:@"提示" alertMessage:@"确定要退出登录吗？" leftCancelTitle:@"取消" leftCancel:nil rightTitle:@"确定" rightOK:^(UIAlertAction *action) {
        [[UserCenter shareCenter]logout:^(BOOL suc,NSString *error) {
            if (suc) {
                [[UserCenter shareCenter] clearUserState];
                LoginNavigationController *login = [[LoginNavigationController alloc]init];
                [self presentViewController:login animated:YES completion:nil];
            }else{
                [DKProgressHUD showErrorWithStatus:error];
            }
        }];
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
